// pages/login/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  onLoad: function(){ 
    wx.showLoading({
      title: '加载中',
    })
    wx.login({
      success: (res) => {
        app.globalData.wxParams.code = res.code;
      }
    })   
    wx.getSetting({
      success: res => {        
        if(!res.authSetting['scope.userInfo']){          
          wx.navigateTo({
            url: '/pages/login/index',
          })          
        }else{
          wx.getUserInfo({
            success: (res) => {
              app.globalData.userInfo = res.userInfo;
              app.globalData.wxParams.encrypted_data = res.encryptedData;
              app.globalData.wxParams.iv = res.iv;
              app.globalData.wxParams.signature = res.signature;
              app.globalData.wxParams.raw_data = res.rawData;

              wx.request({
                url: 'https://sng.tsletu.com/api/wxapp/public/login',
                method: 'get',
                data: app.globalData.wxParams,
                header: {
                  'XX-Device-Type': 'wxapp',
                  'XX-Token': 'xxx'
                },
                success: (res) => {                  
                  app.globalData.token = res.data.data.token;
                  app.globalData.user = res.data.data.user;
                  wx.switchTab({
                    url: '/pages/index/index',
                  })
                }
              })
            }
          })          
        }
      }
    })    
  }  
})